import React, {PureComponent} from "react";
import {View, StyleSheet, TouchableOpacity, Text, Dimensions, Image, Button, Platform} from "react-native";
import color from "../color";
import {ImageButton, CommonTextButton} from "../CustomButton";
import SelectSegmentControl from "./SelectSegmentControl";
import MineInfoFeekback from "../../page/mine/feedback/MineInfoFeekback";
import SafeAreaView from 'react-native-safe-area-view';
const APPBAR_HEIGHT = Platform.OS === 'ios' ? (Dimensions.get('window').height == 2436 ? 88 : 64):56;
const STATUSBAR_HEIGHT = Platform.OS === 'ios' ? 20 : 0;
import {fetchGetRequest} from "../../api/NetworkTool";
import api from "../../api/api";
import method from "../../api/method";
import ShoppingCart from "../../page/mine/shopping/ShoppingCart";
import define from "../../utils/define";
import DeviceStorage from "../../utils/DeviceStorage";
import Tips from "react-native-root-tips";
import Orientation from 'react-native-orientation'


const {height} = Dimensions.get('window')

type Props = {
    callbackSelectCourse:Function,
    callbackSubject:Function,
    subject_title:string,
    navigation:any
}

export default class SelectNavigation extends PureComponent<Props> {

    static navigationOptions = ({navigation}: any) => ({
        header: null
    })

    constructor(props) {
        super(props)
        this.state = {
            topic: [],
            navHeight:64,
        };
    }

    render() {
        return (
            <View style={[styles.container,{height: this.state.navHeight,
            }]}>
                <View style={{alignItems:'center',flexDirection:'row'}}>
                    <CommonTextButton style={styles.select_subject} title={this.props.subject_title} color='white'
                                      onPress={()=>{this.props.callbackSubject(this.props.subject_title)}}/>
                    <View style={{flex:1}}/>
                    <SelectSegmentControl callBackSelectCourse={(index)=>{this.props.callbackSelectCourse(index)}}/>
                    <View style={{flex:1}}/>
                    <View style={{flexDirection:'row',justifyContent:'flex-end',alignItems:'center'}}>
                        <ImageButton style={styles.select_search} onPress={()=>{
                            this.toSearch()
                        }} source={require('../../../res/img/select/select_search_icon.png')}/>
                        <ImageButton style={{width:25,height:22,marginRight:12}} onPress={()=>{
                            this.toShop()
                        }} source={require('../../../res/img/select/select_shop_icon.png')}/>
                    </View>
                </View>
            </View>
        )
    }

    componentDidMount() {
        this.requestNetData()
        if (Platform.OS === 'ios') {
            console.log('res',height)
            if (height == 812){
                this.setState({navHeight:88})

            } else {
                this.setState({navHeight:64})
            }
        }else {
            this.setState({navHeight:56})
        }
    }

    requestNetData() {
        fetchGetRequest(api.hotWords, method.get, null)
            .then(res => {
                console.log(res)
                let tempList = []
                let hotwords = res.hotwords
                if (hotwords.length > 0) {
                    hotwords.map(function (item) {
                        tempList.push(item)
                    });

                    this.setState({
                        topic: tempList
                    })

                } else {
                    this.setState({
                        topic: ['小升初'],
                    })
                }

            })
            .catch(err => {
                this.setState({
                    topic: ['小升初'],
                })
            })
    }

    toSearch() {
        this.props.navigation.navigate('searchScene', {hotwords: this.state.topic})
    }
    toShop() {
        DeviceStorage.get(define.token)
            .then(res=>{
                if (res){
                    this.props.navigation.navigate('ShoppingCart')

                }else {
                    Tips.showWarn('请先登录',{mask:true})
                }
            }).catch(err=>{
        })
    }

}
const styles = StyleSheet.create({
    container: {
        backgroundColor: color.globalMainColor,
        flexDirection: 'row',
        paddingBottom: 10,
        alignItems: 'flex-end',
    },
    select_subject: {
        marginLeft: 12,
    },
    select_ri_li: {
        marginRight: 12,
    },
    select_search: {
        marginRight: 12,
    }
})